angular.module('baas').factory('rest', ['$http', '$q', function ($http, $q) {
    //服务器地址
    var host = "http://127.0.0.1:8080/api/";
    var adminKey = "V1hwT1UyRkhUblZpUjNoclVWTlZlbEpEVlhwU1FTVXpSQ1V6UkElM0QlM0Qc3Rhc";

    var rest = {
        apps: [],
        getTimestamp: function getTimestamp() {
            return new Date().getTime();
        },
        getAdminSign: function getAdminSign(timestamp) {
            return hex_md5(adminKey + ":" + timestamp);
        },
        getMasterSign: function getMasterSign(masterKey, timestamp) {
            return hex_md5(masterKey + ":" + timestamp);
        },
        admin: function (request) {
            var timestamp = rest.getTimestamp();
            var adminSign = rest.getAdminSign(timestamp);
            request.url = host + request.url;
            request.headers = {
                'JB-Plat': 'admin',
                'JB-Timestamp': timestamp,
                'JB-AdminSign': adminSign,
                'Content-Type': 'application/json'
            };
            var deferred = $q.defer();
            $http(request).then(
                function (response) {
                    deferred.resolve(response);
                },
                function (response) {
                    console.dir(response);
                    deferred.reject(response);
                });
            return deferred.promise;
        },
        master: function (request, appId) {
            var deferred = $q.defer();
            var timestamp = rest.getTimestamp();
            rest.getApp(appId)
                .then(
                    function (app) {
                        var masterSign = rest.getMasterSign(app.masterKey, timestamp);
                        request.url = host + request.url;
                        request.headers = {
                            'JB-Plat': 'admin',
                            'JB-Timestamp': timestamp,
                            'JB-AppId': appId,
                            'JB-MasterSign': masterSign,
                            'Content-Type': 'application/json'
                        };
                        return $http(request);
                    })
                .then(
                    function (response) {
                        deferred.resolve(response);
                    },
                    function (response) {
                        console.dir(response);
                        deferred.reject(response);
                    });
            return deferred.promise;
        },
        getApp: function (appId) {
            var deferred = $q.defer();
            if (rest.apps[appId]) {
                deferred.resolve(rest.apps[appId]);
            } else {
                var req = {
                    method: 'GET',
                    url: 'admin/app/' + appId,
                    data: {}
                };
                rest.admin(req).then(function (response) {
                    rest.apps[appId] = response.data;
                    deferred.resolve(rest.apps[appId]);
                }, function (response) {
                    deferred.reject(response.data);
                });
            }
            return deferred.promise;
        }
    };
    return rest;
}]);